package com.jiyi.bean.owner;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.PieData;
import com.swift.maintenance.R;

public class PieChartItem extends ChartItem {

	private SpannableString mCenterText;

	public PieChartItem(ChartData<?> cd, Context c) {
		super(cd);

		mCenterText = generateCenterText();
	}

	@Override
	public int getItemType() {
		return TYPE_PIECHART;
	}

	@Override
	public View getView(int position, View convertView, Context c) {

		ViewHolder holder = null;

		if (convertView == null) {

			holder = new ViewHolder();

			convertView = LayoutInflater.from(c).inflate(
					R.layout.list_item_piechart, null);
			holder.chart = (PieChart) convertView.findViewById(R.id.chart);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.chart.setBackgroundColor(Color.WHITE);
		holder.chart.setUsePercentValues(true);
		holder.chart.getDescription().setEnabled(false);

		holder.chart.setDrawHoleEnabled(true);
		holder.chart.setHoleColor(Color.WHITE);

		holder.chart.setTransparentCircleColor(Color.WHITE);
		holder.chart.setTransparentCircleAlpha(110);

		holder.chart.setHoleRadius(58f);
		holder.chart.setTransparentCircleRadius(61f);

		holder.chart.setDrawCenterText(true);

		holder.chart.setRotationEnabled(false);
		holder.chart.setHighlightPerTapEnabled(true);

		holder.chart.setMaxAngle(180f); // HALF CHART
		holder.chart.setRotationAngle(180f);
		holder.chart.setCenterTextOffset(0, -20);
		holder.chart.setDrawSliceText(false);

		// set data
		holder.chart.setData((PieData) mChartData);

		Legend mLegend = holder.chart.getLegend(); // 设置比例图
		mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); // 左下边显示
		mLegend.setFormSize(12f);// 比例块字体大小
		mLegend.setXEntrySpace(2f);// 设置距离饼图的距离，防止与饼图重合
		mLegend.setYEntrySpace(2f);
		// 设置比例块换行...
		mLegend.setWordWrapEnabled(true);
		mLegend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);

		mLegend.setForm(Legend.LegendForm.SQUARE);// 设置比例块形状，默认为方块
//		getTotalHeightofListView(holder.chart);
		// do not forget to refresh the chart
		// holder.chart.invalidate();
		holder.chart.animateY(900);

		return convertView;
	}

	private SpannableString generateCenterText() {
		SpannableString s = new SpannableString("2017年\n维护保养次数\n统计图");
		// s.setSpan(new RelativeSizeSpan(1.6f), 0, 14, 0);
		// s.setSpan(new ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]),
		// 0, 14, 0);
		// s.setSpan(new RelativeSizeSpan(.9f), 14, 25, 0);
		// s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, 25, 0);
		// s.setSpan(new RelativeSizeSpan(1.4f), 25, s.length(), 0);
		// s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 25,
		// s.length(), 0);
		return s;
	}

	private static class ViewHolder {
		PieChart chart;
	}

	int setHeight(PieChart chart) {
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		chart.measure(w, h);
		int height = chart.getMeasuredHeight();
		return height;
	}

	public static int getTotalHeightofListView(PieChart chart) {
		int totalHeight = 0;
		chart.measure(View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
				.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		totalHeight += chart.getMeasuredHeight();
		ViewGroup.LayoutParams params = chart.getLayoutParams();
		chart.setLayoutParams(params);
		chart.requestLayout();
		return totalHeight;
	}
}
